home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / X11R4 / cmds / X / ddx / Xsun / sunMouse.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-04-23  |  11.6 KB  |  434 lines

  1. /*-
  2.  * sunMouse.c --
  3.  *    Functions for playing cat and mouse... sorry.
  4.  *
  5.  * Copyright (c) 1987 by the Regents of the University of California
  6.  *
  7.  * Permission to use, copy, modify, and distribute this
  8.  * software and its documentation for any purpose and without
  9.  * fee is hereby granted, provided that the above copyright
  10.  * notice appear in all copies.  The University of California
  11.  * makes no representations about the suitability of this
  12.  * software for any purpose.  It is provided "as is" without
  13.  * express or implied warranty.
  14.  *
  15.  *
  16.  */
  17.  
  18. /************************************************************
  19. Copyright 1987 by Sun Microsystems, Inc. Mountain View, CA.
  20.  
  21.                     All Rights Reserved
  22.  
  23. Permission  to  use,  copy,  modify,  and  distribute   this
  24. software  and  its documentation for any purpose and without
  25. fee is hereby granted, provided that the above copyright no-
  26. tice  appear  in all copies and that both that copyright no-
  27. tice and this permission notice appear in  supporting  docu-
  28. mentation,  and  that the names of Sun or MIT not be used in
  29. advertising or publicity pertaining to distribution  of  the
  30. software  without specific prior written permission. Sun and
  31. M.I.T. make no representations about the suitability of this
  32. software for any purpose. It is provided "as is" without any
  33. express or implied warranty.
  34.  
  35. SUN DISCLAIMS ALL WARRANTIES WITH REGARD TO  THIS  SOFTWARE,
  36. INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FIT-
  37. NESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SUN BE  LI-
  38. ABLE  FOR  ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  39. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,  DATA  OR
  40. PROFITS,  WHETHER  IN  AN  ACTION OF CONTRACT, NEGLIGENCE OR
  41. OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
  42. THE USE OR PERFORMANCE OF THIS SOFTWARE.
  43.  
  44. ********************************************************/
  45.  
  46. #ifndef    lint
  47. static char sccsid[] = "%W %G Copyright 1987 Sun Micro";
  48. #endif
  49.  
  50. #define NEED_EVENTS
  51. #include    "sun.h"
  52. #include    "mipointer.h"
  53. #include    "misprite.h"
  54.  
  55. Bool ActiveZaphod = TRUE;
  56.  
  57. static long sunEventTime();
  58. static Bool sunCursorOffScreen();
  59. static void sunCrossScreen();
  60. extern void miPointerQueueEvent();
  61.  
  62. miPointerCursorFuncRec sunPointerCursorFuncs = {
  63.     sunEventTime,
  64.     sunCursorOffScreen,
  65.     sunCrossScreen,
  66.     miPointerQueueEvent,
  67. };
  68.  
  69. typedef struct {
  70.     int        bmask;        /* Current button state */
  71.     Bool    mouseMoved;        /* Mouse has moved */
  72. } SunMsPrivRec, *SunMsPrivPtr;
  73.  
  74. static void           sunMouseCtrl();
  75. static int           sunMouseGetMotionEvents();
  76. static Mouse_Event     *sunMouseGetEvents();
  77. static void           sunMouseProcessEvent();
  78. static void           sunMouseDoneEvents();
  79.  
  80. static SunMsPrivRec    sunMousePriv;
  81.  
  82. static PtrPrivRec     sysMousePriv = {
  83.     -1,                /* Descriptor to device */
  84.     sunMouseGetEvents,        /* Function to read events */
  85.     sunMouseProcessEvent,    /* Function to process an event */
  86.     sunMouseDoneEvents,        /* When all the events have been */
  87.                 /* handled, this function will be */
  88.                 /* called. */
  89.     0,                /* Current x movement of pointer */
  90.     0,                /* Current y movement */
  91.     (pointer)&sunMousePriv,    /* Field private to device */
  92. };
  93.  
  94. /*-
  95.  *-----------------------------------------------------------------------
  96.  * sunMouseProc --
  97.  *    Handle the initialization, etc. of a mouse
  98.  *
  99.  * Results:
  100.  *    none.
  101.  *
  102.  * Side Effects:
  103.  *
  104.  * Note:
  105.  *    When using sunwindows, all input comes off a single fd, stored in the
  106.  *    global windowFd.  Therefore, only one device should be enabled and
  107.  *    disabled, even though the application still sees both mouse and
  108.  *    keyboard.  We have arbitrarily chosen to enable and disable windowFd
  109.  *    in the keyboard routine sunKbdProc rather than in sunMouseProc.
  110.  *
  111.  *    In Sprite, things work like under suntools, in that /dev/mouse
  112.  *    handles both mouse and keyboard.
  113.  *
  114.  *-----------------------------------------------------------------------
  115.  */
  116. int
  117. sunMouseProc (pMouse, what)
  118.     DevicePtr      pMouse;       /* Mouse to play with */
  119.     int              what;            /* What to do with it */
  120. {
  121.     register int  fd;
  122.     int              format;
  123.     static int      oformat;
  124.     BYTE          map[4];
  125.  
  126.     switch (what) {
  127.     case DEVICE_INIT:
  128.         if (pMouse != LookupPointerDevice()) {
  129.         ErrorF ("Cannot open non-system mouse");    
  130.         return (!Success);
  131.         }
  132.  
  133.         sunMousePriv.bmask = 0;
  134.         sunMousePriv.mouseMoved = FALSE;
  135.         sysMousePriv.dx = 0;
  136.         sysMousePriv.dy = 0;
  137.  
  138.         pMouse->devicePrivate = (pointer) &sysMousePriv;
  139.         pMouse->on = FALSE;
  140.         map[1] = 1;
  141.         map[2] = 2;
  142.         map[3] = 3;
  143.         InitPointerDeviceStruct(
  144.         pMouse, map, 3, sunMouseGetMotionEvents, sunMouseCtrl, 0);
  145.         break;
  146.  
  147.     case DEVICE_ON:
  148.         pMouse->on = TRUE;
  149.         break;
  150.  
  151.     case DEVICE_CLOSE:
  152.         break;
  153.  
  154.     case DEVICE_OFF:
  155.         pMouse->on = FALSE;
  156.         break;
  157.     }
  158.     return (Success);
  159. }
  160.         
  161. /*-
  162.  *-----------------------------------------------------------------------
  163.  * sunMouseCtrl --
  164.  *    Alter the control parameters for the mouse. Since acceleration
  165.  *    etc. is done from the PtrCtrl record in the mouse's device record,
  166.  *    there's nothing to do here.
  167.  *
  168.  * Results:
  169.  *    None.
  170.  *
  171.  * Side Effects:
  172.  *    None.
  173.  *
  174.  *-----------------------------------------------------------------------
  175.  */
  176. /*ARGSUSED*/
  177. static void
  178. sunMouseCtrl (pMouse)
  179.     DevicePtr      pMouse;
  180. {
  181. }
  182.  
  183. /*-
  184.  *-----------------------------------------------------------------------
  185.  * sunMouseGetMotionEvents --
  186.  *    Return the (number of) motion events in the "motion history
  187.  *    buffer" (snicker) between the given times.
  188.  *
  189.  * Results:
  190.  *    The number of events stuffed.
  191.  *
  192.  * Side Effects:
  193.  *    The relevant xTimecoord's are stuffed in the passed memory.
  194.  *
  195.  *-----------------------------------------------------------------------
  196.  */
  197. /*ARGSUSED*/
  198. static int
  199. sunMouseGetMotionEvents (buff, start, stop, pScreen)
  200.     CARD32 start, stop;
  201.     xTimecoord *buff;
  202.     ScreenPtr pScreen;
  203. {
  204.     return 0;
  205. }
  206.  
  207. /*-
  208.  *-----------------------------------------------------------------------
  209.  * sunMouseGetEvents --
  210.  *    Return the events waiting in the wings for the given mouse.
  211.  *
  212.  * Results:
  213.  *    A pointer to an array of Mouse_Events or (Mouse_Event *)0 if no events
  214.  *    The number of events contained in the array.
  215.  *    A boolean as to whether more events might be available.
  216.  *
  217.  * Side Effects:
  218.  *    None.
  219.  *-----------------------------------------------------------------------
  220.  */
  221. static Mouse_Event *
  222. sunMouseGetEvents (pMouse, pNumEvents)
  223.     DevicePtr      pMouse;        /* Mouse to read */
  224.     int              *pNumEvents;        /* Place to return number of events */
  225. {
  226.     return (Mouse_Event *)0;
  227. }
  228.  
  229.  
  230. /*-
  231.  *-----------------------------------------------------------------------
  232.  * MouseAccelerate --
  233.  *    Given a delta and a mouse, return the acceleration of the delta.
  234.  *
  235.  * Results:
  236.  *    The corrected delta
  237.  *
  238.  * Side Effects:
  239.  *    None.
  240.  *
  241.  *-----------------------------------------------------------------------
  242.  */
  243. static short
  244. MouseAccelerate (pMouse, delta)
  245.     DevicePtr      pMouse;
  246.     int              delta;
  247. {
  248.     register int  sgn = sign(delta);
  249.     register PtrCtrl *pCtrl;
  250.  
  251.     delta = abs(delta);
  252.     pCtrl = &((DeviceIntPtr) pMouse)->ptrfeed->ctrl;
  253.  
  254.     if (delta > pCtrl->threshold) {
  255.     return ((short) (sgn * (pCtrl->threshold +
  256.                 ((delta - pCtrl->threshold) * pCtrl->num) /
  257.                 pCtrl->den)));
  258.     } else {
  259.     return ((short) (sgn * delta));
  260.     }
  261. }
  262.  
  263. /*-
  264.  *-----------------------------------------------------------------------
  265.  * sunMouseProcessEvent --
  266.  *    Given a Firm_event for a mouse, pass it off the the dix layer
  267.  *    properly converted...
  268.  *
  269.  * Results:
  270.  *    None.
  271.  *
  272.  * Side Effects:
  273.  *    The cursor may be redrawn...? devPrivate/x/y will be altered.
  274.  *
  275.  *-----------------------------------------------------------------------
  276.  */
  277. static void
  278. sunMouseProcessEvent (pMouse, ev)
  279.     DevicePtr      pMouse;       /* Mouse from which the event came */
  280.     Mouse_Event      *ev;            /* Event to process */
  281. {
  282.     xEvent        xE;
  283.     register PtrPrivPtr    pPriv;    /* Private data for pointer */
  284.     register SunMsPrivPtr pSunPriv; /* Private data for mouse */
  285.     register int      bmask;    /* Temporary button mask */
  286.     register int      button;
  287.  
  288.     pPriv = (PtrPrivPtr)pMouse->devicePrivate;
  289.     pSunPriv = (SunMsPrivPtr) pPriv->devPrivate;
  290.  
  291.     xE.u.keyButtonPointer.time = ev->time;
  292.     bmask = ev->key ^ pSunPriv->bmask;
  293.  
  294.     /*
  295.      * When we detect a change in the mouse coordinates, we call
  296.      * the cursor module to move the cursor. It has the option of
  297.      * simply removing the cursor or just shifting it a bit.
  298.      * If it is removed, DIX will restore it before we goes to sleep...
  299.      *
  300.      * What should be done if it goes off the screen? Move to another
  301.      * screen? For now, we just force the pointer to stay on the
  302.      * screen...
  303.      *
  304.      * For some reason, motion up generates a positive y delta
  305.      * and motion down a negative delta, so we must subtract
  306.      * here instead of add...
  307.      */
  308.     if (ev->deltaX) {
  309.         pPriv->dx += MouseAccelerate (pMouse, ev->deltaX);
  310.         ((SunMsPrivPtr)pPriv->devPrivate)->mouseMoved = TRUE;
  311.     }
  312.  
  313.     if (ev->deltaY) {
  314.         pPriv->dy -= MouseAccelerate (pMouse, ev->deltaY);
  315.         ((SunMsPrivPtr)pPriv->devPrivate)->mouseMoved = TRUE;
  316.     }
  317.  
  318.     if (ev->key ^ pSunPriv->bmask) {
  319.     sunMouseDoneEvents (pMouse, FALSE);
  320.     }
  321.  
  322.     xE.u.keyButtonPointer.time = ev->time;
  323.     for (bmask = 4, button = 1; bmask != 0; bmask >>= 1, button++) {
  324.     if ((ev->key & bmask) != (pSunPriv->bmask & bmask)) {
  325.         xE.u.u.type = (ev->key & bmask) ? ButtonRelease : ButtonPress;
  326.         xE.u.u.detail = button;
  327.  
  328.         miPointerPosition (screenInfo.screens[0],
  329.         &xE.u.keyButtonPointer.rootX,
  330.         &xE.u.keyButtonPointer.rootY);
  331.  
  332.         (* pMouse->processInputProc) (&xE, pMouse, 1);
  333.     }
  334.     }
  335.     pSunPriv->bmask = ev->key;
  336.     lastEventTime = ev->time;
  337. }
  338.  
  339. /*ARGSUSED*/
  340. static Bool
  341. sunCursorOffScreen (pScreen, x, y)
  342.     ScreenPtr    *pScreen;
  343.     int        *x, *y;
  344. {
  345.     int        index;
  346.  
  347.     /*
  348.      * Active Zaphod implementation:
  349.      *    increment or decrement the current screen
  350.      *    if the x is to the right or the left of
  351.      *    the current screen.
  352.      */
  353.     if (ActiveZaphod &&
  354.     screenInfo.numScreens > 1 && (*x >= (*pScreen)->width || *x < 0))
  355.     {
  356.     index = (*pScreen)->myNum;
  357.     if (*x < 0)
  358.     {
  359.         index = (index ? index : screenInfo.numScreens) - 1;
  360.         *pScreen = screenInfo.screens[index];
  361.         *x += (*pScreen)->width;
  362.     }
  363.     else
  364.     {
  365.         *x -= (*pScreen)->width;
  366.         index = (index + 1) % screenInfo.numScreens;
  367.         *pScreen = screenInfo.screens[index];
  368.     }
  369.     return TRUE;
  370.     }
  371.     return FALSE;
  372. }
  373.  
  374. /*ARGSUSED*/
  375. static long
  376. sunEventTime (pScreen)
  377.     ScreenPtr    pScreen;
  378. {
  379.     return lastEventTime;
  380. }
  381.  
  382. static void
  383. sunCrossScreen (pScreen, entering)
  384.     ScreenPtr    pScreen;
  385.     Bool    entering;
  386. {
  387.     unsigned char  select;
  388.  
  389.     select = 1;
  390.     if (entering)
  391.     select = 0;
  392.     if (sunFbs[pScreen->myNum].EnterLeave)
  393.     (*sunFbs[pScreen->myNum].EnterLeave) (pScreen, select);
  394. }
  395.  
  396. /*-
  397.  *-----------------------------------------------------------------------
  398.  * sunMouseDoneEvents --
  399.  *    Finish off any mouse motions we haven't done yet. (At the moment
  400.  *    this code is unused since we never save mouse motions as I'm
  401.  *    unsure of the effect of getting a keystroke at a given [x,y] w/o
  402.  *    having gotten a motion event to that [x,y])
  403.  *
  404.  * Results:
  405.  *    None.
  406.  *
  407.  * Side Effects:
  408.  *    A MotionNotify event may be generated.
  409.  *
  410.  *-----------------------------------------------------------------------
  411.  */
  412. /*ARGSUSED*/
  413. static void
  414. sunMouseDoneEvents (pMouse,final)
  415.     DevicePtr      pMouse;
  416.     Bool      final;
  417. {
  418.     PtrPrivPtr      pPriv;
  419.     SunMsPrivPtr  pSunPriv;
  420.     int          dx, dy;
  421.  
  422.     pPriv = (PtrPrivPtr) pMouse->devicePrivate;
  423.     pSunPriv = (SunMsPrivPtr) pPriv->devPrivate;
  424.  
  425.     if (pSunPriv->mouseMoved) {
  426.     dx = pPriv->dx;
  427.     dy = pPriv->dy;
  428.     pPriv->dx = 0;
  429.     pPriv->dy = 0;
  430.     pSunPriv->mouseMoved = FALSE;
  431.     miPointerDeltaCursor (screenInfo.screens[0], dx, dy, TRUE);
  432.     }
  433. }
  434.